package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.CommentOnCommodityMapper;
import com.guigu.mapper.OrdersMapper;
import com.guigu.pojo.CommentOnCommodity;
import com.guigu.pojo.EvaluateDetail;
import com.guigu.pojo.Orders;
import com.guigu.service.CommentOnCommodityService;
import com.guigu.service.EvaluateDetailService;
import com.guigu.service.OrdersService;
import com.guigu.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CommentOnCommodityServiceImpl extends
        ServiceImpl<CommentOnCommodityMapper, CommentOnCommodity>
implements CommentOnCommodityService {

    @Autowired
    CommentOnCommodityMapper commodityMapper; //评价表

    @Autowired
    OrdersMapper ordersMapper; //订单Service

    @Autowired
    EvaluateDetailService detailService; //详情表

    @Autowired
    UsersService userService; //用户Service
    @Override
    public Map addCommodity(CommentOnCommodity commodity) {
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","评价失败!");
        Orders orders = ordersMapper.selectById(commodity.getOid());
        commodity.setUid(orders.getUId());
        commodity.setEtime(new Date());
        commodity.setIsDelete(0);
        boolean b1 = this.save(commodity);
        if (b1){
            for (EvaluateDetail evaluateDetail : commodity.getDetailList()) {
                evaluateDetail.setPid(commodity.getId());
                evaluateDetail.setGid(evaluateDetail.getShopInfo().getId());
            }
            boolean b2 = detailService.saveBatch(commodity.getDetailList());
           if (b2){
               //修改订单为已评价
               Orders od=new Orders();
               od.setId(commodity.getOid());
               od.setOrderState("o-8");
               ordersMapper.updateById(od);
               map.put("code",1);
               map.put("msg","评价成功!");
           }
        }
        return map;
    }

    //id查询对象
    @Override
    public CommentOnCommodity queryById(Integer pid) {
        CommentOnCommodity comment = this.getById(pid);
        comment.setUser(userService.getById(comment.getUid()));
        return comment;
    }

    //分页及所有
    @Override
    public Page<CommentOnCommodity> queryAllComment(CommentOnCommodity comment, Integer pageno, Integer pagesize) {
        QueryWrapper<CommentOnCommodity> queryWrapper=
                new  QueryWrapper<CommentOnCommodity>();
        if (comment.getUid()!=null){
            queryWrapper.eq("uid",comment.getUid());
        }
        Page<CommentOnCommodity> page = this.page(new Page<CommentOnCommodity>(pageno, pagesize), queryWrapper);
        for (CommentOnCommodity record : page.getRecords()) {
            record.setDetailList(detailService.queryEvaluatesByPid(record.getId()));
        }
        return page;
    }


}
